'''
	In this module the basic components of recordings is stored. Recordings are
	made up out of two parts. An atom and a recording. An atom is a single
	datapoint in the recording.  A recording is a collection of a atoms. 

	Recordings are always stored as CSV for simplicity. 
		
	@author: J.H. Donker
	@copyright: Twente University
'''

import time


class RecordingAtom:
	'''
		An Atom contains always an attribute with a type and a timestamp. In
		normal operation the the attribute is a tuple with three floats. And the 
		type is an integer that can represent orientation, position and scale. 
		
		Each atom is timestamped during operation. 
	'''
	ORIENTATION = 0
	POSITION = 1
	SCALE = 2
	
	def __init__(self, value, type, timestamp = -1):
		'''
			Creates a new geom. 
			
			@param value: The tuple that will contain the data
			@param type: The type of the data. 
			@param timestamp: A timestamp, defaults to the current time. 
		'''
		self.__value = value
		self.__type = type
		if timestamp == -1:
			self.__timestamp = time.time()	
		else:
			self.__timestamp = timestamp
	def GetType(self):
		'''
			Returns the type of the data.
			
			@return: The data type. 
		'''
		return self.__type

	def GetValue(self):
		'''
			Returns the data.
			
			@return: The data. 
		'''
		return self.__value
		
	def GetTimeStamp(self):
		'''
			Returns the timestamp.
			
			@return: The timestamp.
		'''
		return self.__timestamp
	
	def ToString(self):
		'''
			Returns the the content of the atom in a csv formated string.
			
			@return: A CSV formated string.
		'''
		string = str(self.__value[0]) + ", " +  str(self.__value[1]) + ", " + str(self.__value[2])
		string = string  + ", " +  str(self.__type) + ", " + str(self.__timestamp) + "\n"
		return string

class Recording:
	'''
		A recording works as a collection of atoms. 
	'''
	def __init__(self):
	
		self.__atoms	= []

	
	def AddAtom(self, atom):
		'''
			Adds a number of an atom to the recording. 
			
			@param atom: Atom to be added. 
		'''
		self.__atoms.append(atom)
	
	def GetNumberOfAtoms(self):
		'''
			Returns the number of atoms in the recording. 

			@return: Number of atoms.
		'''
		return len(self.__atoms)
	
	def GetAtom(self, index):
		'''
			Returns the an atom.
			
			@param index: The index of the desired atom.
			@return: The atom if in range
		'''
		return self.__atoms[index]
	
	
	def Save(self):
		'''
			Writes the entire geom as an CSV formated string.
			
			@return: CSV formated string. 
		'''
		str = '';
		for atom in self.__atoms:
			#str = join( (str, atom.ToString()), "\n")
			str = str + atom.ToString() + "\n"
		return str
	
	def CalculateLength(self):
		'''
			Calculates the length of the recording.
			
			@return: the length of the recording. 
		'''
		length = len(self.__atoms)
		
		if length >= 2:
			return (self.__atoms[length - 1].GetTimeStamp() - self.__atoms[0].GetTimeStamp())
		else:
			return 0
	
	def Load(self, CSV):
		'''
			Loads a the collection from a csv formated string. 
			
			@param CSV: A csv formated string.
		'''
		ar = CSV.split("\n")
		for atomString in ar:
			print atomString
			atom = self._AtomFromString(atomString)
			if atom != None:
				self.AddAtom(atom)
				
	def ToString(self):
		'''
			Returns a CSV formated string of the collection
			
			@return: A CSV formated string. 
		'''
		str = ""
		for atom in self.__atoms:
			str = str + atom.ToString()
		return str
	
	def _AtomFromString(self, atomString):
		'''
			Makes an atom out of a csv string.
			
			@param atomString: The csv string to be converted. 
			@return: An atom. 
		'''
		print "_atomfromstring"
		lst = atomString.split(',')
		if len(lst) == 5:
			return RecordingAtom( (float(lst[0]), float(lst[1]), float(lst[2])), int(lst[3]), float(lst[4]) )
		else:
			return None